package com.xjh.him.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;
import com.scwang.smartrefresh.layout.adapter.SmartViewHolder;
import com.xjh.him.R;
import com.xjh.him.core.WeChatPresenter;
import com.xjh.him.dto.trend.TrendDTO;
import com.xjh.him.utils.DateUtils;
import com.xjh.him.utils.Utils;
import com.xuexiang.xui.utils.DensityUtils;
import com.ypx.imagepicker.ImagePicker;
import com.ypx.imagepicker.bean.ImageItem;
import com.ypx.imagepicker.data.OnImagePickCompleteListener;

import org.xjh.tool.Checks;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

public class TrendAdapter extends RecyclerView.Adapter<SmartViewHolder>{
    private Activity mActivity;
    private List<TrendDTO> trendDTOList;

    public TrendAdapter(Activity activity, List<TrendDTO> trendDTOList) {
        mActivity = activity;
        this.trendDTOList = trendDTOList;
    }

    @NonNull
    @Override
    public SmartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_trend_item, parent, false);
        return new SmartViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SmartViewHolder holder, int position) {
        TrendDTO trendDTO = trendDTOList.get(position);
        Glide.with(mActivity)
                .load(trendDTO.getUserAvatar())
                .apply(Utils.getRoundedCornersRequestOptions(5))
                .into((ImageView) holder.findViewById(R.id.iv_item_avatar));
        holder.text(R.id.tv_item_nickname, trendDTO.getUserNickname());
        holder.text(R.id.tv_item_content, trendDTO.getTextContent());

        List<String> imgList = trendDTO.getImgList();
        if (!Checks.isEmpty(imgList)) {
            int widthPixels = DensityUtils.getDisplayMetrics().widthPixels;
            int pixelsSize = ((widthPixels - DensityUtils.dp2px(80)) / imgList.size());
            pixelsSize = Math.min(pixelsSize, widthPixels / 2);
            for (int i = 0; i < imgList.size(); i++) {
                ImageView imageView = null;
                if (i == 0) {
                    imageView = holder.findViewById(R.id.iv_item_img1);
                    holder.findViewById(R.id.iv_item_img2).setVisibility(View.GONE);
                    holder.findViewById(R.id.iv_item_img3).setVisibility(View.GONE);
                }else if (i == 1) {
                    imageView = holder.findViewById(R.id.iv_item_img2);
                    holder.findViewById(R.id.iv_item_img3).setVisibility(View.GONE);
                }else if (i == 2) {
                    imageView = holder.findViewById(R.id.iv_item_img3);
                }
                handleImageView(imageView, pixelsSize, imgList, i);
            }
            holder.findViewById(R.id.ll_img_layout).setVisibility(View.VISIBLE);
        }else {
            holder.findViewById(R.id.iv_item_img1).setVisibility(View.GONE);
            holder.findViewById(R.id.iv_item_img2).setVisibility(View.GONE);
            holder.findViewById(R.id.iv_item_img3).setVisibility(View.GONE);
            holder.findViewById(R.id.ll_img_layout).setVisibility(View.GONE);
        }

        String timeString = DateUtils.getIntervalTime(trendDTO.getCreateTime());
        holder.text(R.id.tv_item_time, timeString);
    }

    private void handleImageView(ImageView imageView, int pixelsSize, List<String> imgList, int i) {
        //根据屏幕大小设置宽高
        ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
        layoutParams.width = pixelsSize;
        layoutParams.height = pixelsSize;
        imageView.setLayoutParams(layoutParams);

        //加载图片
        imageView.setVisibility(View.VISIBLE);
        Glide.with(mActivity)
                .load(imgList.get(i))
                .into(imageView);

        //图片设置点击预览
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> pathList = new ArrayList<>();
                CountDownLatch latch = new CountDownLatch(imgList.size());
                for (int i = 0; i < imgList.size(); i++) {
                    String imgUrl = imgList.get(i);
                    new Thread(() -> {
                        try {
                            FutureTarget<File> futureTarget = Glide.with(mActivity)
                                    .downloadOnly()
                                    .load(imgUrl)
                                    .submit();
                            File file = futureTarget.get();
                            if (file != null) {
                                pathList.add(file.getAbsolutePath());
                            }
                        } catch (InterruptedException | ExecutionException e) {
                            e.printStackTrace();
                        } finally {
                            latch.countDown();
                        }
                    }).start();
                }

                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                ImagePicker.preview(mActivity, new WeChatPresenter(), pathList, 0, new OnImagePickCompleteListener() {
                    @Override
                    public void onImagePickComplete(ArrayList<ImageItem> items) {

                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return trendDTOList.size();
    }

    public void setTrendDTOList(List<TrendDTO> trendDTOList) {
        this.trendDTOList = trendDTOList;
    }
}
